package io.github.wslxm.springbootplus2.manage.sys.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.manage.sys.dao.SysRoleAuthDao;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysRoleAuthMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysRoleAuth;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * base--sys--角色/用户关联 ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 *
 * @author ws
 * @email 1720696548@qq.com
 * @date 2025-09-17 16:58:07
 */
@Service
public class SysRoleAuthDaoImpl extends BaseServiceImpl<SysRoleAuthMapper, SysRoleAuth> implements SysRoleAuthDao {

    @Override
    public List<SysRoleAuth> findAuthByRoleId(String roleId) {
        return baseMapper.selectList(new LambdaQueryWrapper<SysRoleAuth>().eq(SysRoleAuth::getRoleId, roleId));
    }

    @Override
    public List<String> findAuthIdsByRoleId(String roleId) {
        List<SysRoleAuth> sysRoleAuths = baseMapper.selectList(new LambdaQueryWrapper<SysRoleAuth>().eq(SysRoleAuth::getRoleId, roleId));
        return sysRoleAuths.stream().map(SysRoleAuth::getAuthId).collect(Collectors.toList());
    }

    @Override
    public List<String> findAuthIdsByRoleIds(List<String> roleIds) {
        List<SysRoleAuth> sysRoleAuths = baseMapper.selectList(new LambdaQueryWrapper<SysRoleAuth>().in(SysRoleAuth::getRoleId, roleIds));
        return sysRoleAuths.stream().map(SysRoleAuth::getAuthId).collect(Collectors.toList());
    }

    @Override
    public boolean addRoleAuth(String roleId, String authId) {
        return this.save(new SysRoleAuth(roleId, authId));
    }

    @Override
    public boolean updRoleAuths(String roleId, List<String> authIds) {
        this.delByRoleId(roleId);
        if (authIds == null || authIds.size() <= 0) {
            return true;
        }
        List<SysRoleAuth> roleMenus = new ArrayList<>();
        authIds.forEach(menuId -> roleMenus.add(new SysRoleAuth(roleId, menuId)));
        return this.saveBatch(roleMenus);
    }

    @Override
    public boolean delByAuthIds(List<String> authIds) {
        return this.remove(new LambdaQueryWrapper<SysRoleAuth>().in(SysRoleAuth::getAuthId, authIds));
    }

    @Override
    public boolean delByRoleId(String roleId) {
        return this.remove(new LambdaQueryWrapper<SysRoleAuth>().eq(SysRoleAuth::getRoleId, roleId));
    }
}
